/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package spidercore;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLConnection;
import java.util.Date;
import java.util.zip.GZIPInputStream;

/**
 *
 * @author Lone_Wolf
 */
public class ConnectionTools {

    public ConnectionTools() {
    }

    public void printHeaders(URLConnection conn, boolean print) {
        boolean continuer = true;
        int i = 0;
        while (continuer) {
            String headerName = conn.getHeaderFieldKey(i);
            String headerValue = conn.getHeaderField(i);

            if (headerName == null && headerValue == null) {
                // No more headers
                continuer = false;
                if (print) {
                    System.out.println("");
                }
            } else {
                i++;
                if (print) {
                    System.out.println(headerName + ": " + headerValue);
                }
            }
        }
    }

    public String getContent(URLConnection conn, boolean print, boolean gzip) {
        String s;
        String content = "";
        String enter = "\n";
        BufferedReader br;

        try {
            if (gzip) {
                br = new BufferedReader(new InputStreamReader(new GZIPInputStream(conn.getInputStream())));

            } else {
                br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            }
            if (print) {
                while ((s = br.readLine()) != null) {
                    System.out.println(s);
                    content += s + "\n";
                }
            } else {
                long temps = new Date().getTime();
                while ((s = br.readLine()) != null) {
                    content += s + enter;
                }
            }
            br.close();

        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        return content;
    }
}
